var workflowConfigListVM = {
	_FirstLoadDone: false,

	_WorkflowMapping: {
		'workflows': {
			// option holds options.data with the actual data
			// and options.parent with a reference to the parent object
			create: function( options ) {
				return new workflowModel( options.data, workflowConfigListVM );
			},
			key: function(data) {
				return ko.utils.unwrapObservable(data.id);
			}
		}
	},

	// contains id of currently selected workflow
	selectedWorkflow: ko.observable(null),

	// checks whether a certain workflow is selected. called by template
	isWorkflowSelected: function( id ){
		return (id == workflowConfigListVM.selectedWorkflow());
	},

	disableWorkflows: function( ruleClassId ) {
		$.each(this.workflows(), function( index, workflow){
			if (workflow.rule_class_id() === ruleClassId ) {
				workflow.enabled(false);
			}
		});
	},

	loadWorkflows: function( ruleClassId ) {
		// need to save the reference to this so that
		// it can be used in the ajax callback.
		var that = this;
		var endpoint = '/workflows';
		var firstLoadDone = false;

		if (ruleClassId) {
			endpoint += '/' + ruleClassId;
		}

		// get data
		var xhr = $.wGet(endpoint, null);
		xhr.success( function( data, textStatus, jqXhr ) {

			if (that._FirstLoadDone) {
				ko.mapping.updateFromJS(that, { workflows: data });
			} else {
				ko.mapping.fromJS({ workflows: data }, that._WorkflowMapping, that);
				ko.applyBindings(workflowConfigListVM);
				that._FirstLoadDone = true;
			}
		});
		return xhr;
	}

};

var workflowModel = function( data, viewModel ) {
	ko.mapping.fromJS(data, { 'steps': { create: function(options){ return new stepModel( options.data ) }}}, this);

	this.viewModel = viewModel;

	this.ruleClassName = ko.dependentObservable( function() {
		var ruleClassName = "";
		switch( this.rule_class_id() ) {
			case 1:
				ruleClassName = "Apollo Test Config";
				break;
			case 2:
				ruleClassName = "Sabre Test Config";
				break;
			case 3:
				ruleClassName = "Worldspan Test Config";
				break;
			case 4:
				ruleClassName = "Amadeus Test Config";
				break;
		}
		return ruleClassName;
	}, this);

	// set id of selected workflow in workflowConfigListVM
	this.selectWorkflow = function() {
		viewModel.selectedWorkflow(this.id());
		return false; // prevent event bubbling
	};

	this.enableWorkflow = function() {
		var that = this;

		$.wPost('/workflow/' + that.id() + '/enable', null).success(function(data, textStatus, jqXhr){
			viewModel.disableWorkflows( that.rule_class_id() );
			that.enabled(true);
		});
	};

	this.stepsInWords = function() {
		var part = '';

		$.each(this.steps(), function(index, json){
			var step = $.parseJSON(json);

			if (index > 0) {
				part += '<p>then ';
			} else {
				part += '<p>';
			}

			for (var stepClass in step.step) {
				switch(stepClass) {
					case 'Workflow::ApproveStep':
						part += 'Requires approval by ';
						part += '<span class="approver-configurator configurator">';
						part += step.step[stepClass].approver;
						part += '</span>';
						part += '</p>';
						break;
					case 'Workflow::EmailStep':
						part += 'Send email from ';
						part += '<span class="approver-configurator configurator">';
						part += step.step[stepClass].from;
						part += '</span>';
						part += ' to ';
						part += '<span class="email-configurator configurator">';
						part += step.step[stepClass].to;
						part += '</span>';
						part += '</p>';

						break;
					case 'Workflow::IfElseStep':
						break;
					case 'Workflow::BeginStep':
						break;
					case 'Workflow::EndStep':
						break;
					case 'Workflow::GotoStep':
						break;
				}
			}
		});
	return part;
	};
}

var stepModel = function( data ){
	ko.mapping.fromJS(data, {}, this);
};